package GUI;

import entities.CProcessRequest;
import MessagePacks.*;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.JTextArea;
import java.awt.ComponentOrientation;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import common.MyFile;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class NewRequestScreen extends MasterGUI {
		
	private int initiatorID;
	private JComboBox selectSyscomboBox=null;
	private JTextArea curSitTxtArea;
	private JTextArea reqChangeTxtArea;
	private JTextArea reasonTxtArea;
	private JTextArea commentTxtArea;
	private CProcessRequest p;
	private ImageIcon imageOK;
	private ImageIcon imageNot;
	private MainGUI mainGui;
	private JButton btnAttachFile;
	private JButton btnCancel;
	private JButton btnSend;
	private JPanel newRequestMenuPanel;
	private JPanel newRequestPanel;
	public String filename;
	private File selectedFile;
	private MyFile reqFile;
	private int fileFlag;
	private String fileEnd;
	//NewRequestPack reqPack;
	private static final long serialVersionUID = 1L;
	
	public NewRequestScreen(int initID,MainGUI mainGui) {
		super(mainGui);
		this.mainGui=mainGui;
		initiatorID=initID;
		this.fileFlag=0;
		
		getContentPane().setLayout(null);
		JLabel lblWelcomeToIcm = new JLabel("New Request");
		lblWelcomeToIcm.setBounds(0, 0, 884, 42);
		lblWelcomeToIcm.setFont(new Font("Trajan Pro", Font.BOLD, 16));
		lblWelcomeToIcm.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeToIcm.setAlignmentX(Component.CENTER_ALIGNMENT);
		getContentPane().add(lblWelcomeToIcm);

		newRequestMenuPanel = new JPanel();
		newRequestMenuPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		newRequestMenuPanel.setBounds(0, 40, 123, 92);
		getContentPane().add(newRequestMenuPanel);
		newRequestMenuPanel.setLayout(null);

		setBtnAttachFile( btnAttachFile);

		newRequestPanel = new JPanel();
		newRequestPanel.setBorder(UIManager.getBorder("ToolTip.border"));
		newRequestPanel.setBounds(207, 40, 635, 508);
		getContentPane().add(newRequestPanel);
		newRequestPanel.setLayout(null);

		JLabel lblPrivilegedInformationSystem = new JLabel("Privileged information system");
		lblPrivilegedInformationSystem.setFont(new Font("Tahoma", Font.PLAIN,15));
		lblPrivilegedInformationSystem.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPrivilegedInformationSystem.setBounds(10, 15, 260, 20);
		newRequestPanel.add(lblPrivilegedInformationSystem);

		JLabel lblCurrentSituationDescription = new JLabel("Current Situation Description ");
		lblCurrentSituationDescription.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblCurrentSituationDescription.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCurrentSituationDescription.setBounds(10, 50, 260, 20);
		newRequestPanel.add(lblCurrentSituationDescription);

		JLabel lblRequestedChangeDescription = new JLabel("Requested Change Description ");
		lblRequestedChangeDescription.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblRequestedChangeDescription.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRequestedChangeDescription.setBounds(10, 140, 260, 20);
		newRequestPanel.add(lblRequestedChangeDescription);

		JLabel lblComments = new JLabel("Comments");
		lblComments.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblComments.setHorizontalAlignment(SwingConstants.RIGHT);
		lblComments.setBounds(10, 320, 260, 20);
		newRequestPanel.add(lblComments);

		JLabel lblReasonForRequest = new JLabel("Reason For Request");
		lblReasonForRequest.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblReasonForRequest.setHorizontalAlignment(SwingConstants.RIGHT);
		lblReasonForRequest.setBounds(10, 230, 260, 20);
		newRequestPanel.add(lblReasonForRequest);
		
		curSitTxtArea = new JTextArea("Describe The Current Situation");
		JScrollPane sp = new JScrollPane();
		sp.setWheelScrollingEnabled(true);
		sp.setBounds(276, 56, 195, 63);
		newRequestPanel.add(sp);
		sp.setViewportView(curSitTxtArea);
		curSitTxtArea.setWrapStyleWord(true);
		curSitTxtArea.setLineWrap(true);
		curSitTxtArea.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		JScrollPane sp1 = new JScrollPane();
		sp1.setWheelScrollingEnabled(true);
		sp1.setBounds(276, 144, 195, 63);
		newRequestPanel.add(sp1);						
		reqChangeTxtArea = new JTextArea("Describe The Wanted Change");
		sp1.setViewportView(reqChangeTxtArea);
		reqChangeTxtArea.setWrapStyleWord(true);
		reqChangeTxtArea.setLineWrap(true);
		reqChangeTxtArea.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		JScrollPane sp2 = new JScrollPane();
		sp2.setWheelScrollingEnabled(true);
		sp2.setBounds(276, 228, 195, 63);
		newRequestPanel.add(sp2);										
		reasonTxtArea = new JTextArea("Descibe The Reason For Change");
		sp2.setViewportView(reasonTxtArea);
		reasonTxtArea.setWrapStyleWord(true);
		reasonTxtArea.setLineWrap(true);
		reasonTxtArea.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		JScrollPane sp3 = new JScrollPane();
		sp3.setWheelScrollingEnabled(true);
		sp3.setBounds(276, 320, 195, 63);
		newRequestPanel.add(sp3);													
		commentTxtArea = new JTextArea("Mention Extra Information, If Needed");
		sp3.setViewportView(commentTxtArea);
		commentTxtArea.setWrapStyleWord(true);
		commentTxtArea.setLineWrap(true);
		commentTxtArea.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
																		
		newRequestPanel.add(getSyscomboBox());  //Add The ComboBox To The newRequestPanel
		
		setBtnSend(btnSend);

		
		/*JLabel lblOkOk = new JLabel("");
		lblOkOk.setBounds(496, 11, 33, 32);
		newRequestPanel.add(lblOkOk);
		
		imageOK = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.jpg"));
		ImageIcon OK = new ImageIcon(imageOK.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		lblOkOk.setIcon(OK);
		newRequestPanel.add(lblOkOk);*/
	}
	
	/**
	 * This method initializes selectSyscomboBox	
	 * @return
	 */
	private JComboBox getSyscomboBox(){
		selectSyscomboBox = new JComboBox();
		selectSyscomboBox.setModel(new DefaultComboBoxModel(new String[] {"Select A System","Library", "Moodle", "Website", "Information Station", "Computers"}));
		selectSyscomboBox.setToolTipText("Choose the system");
		selectSyscomboBox.setBounds(280, 17, 191, 20);
		selectSyscomboBox.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent e) {
				System.out.println("ComboBoxitemStateChanged()"); 
			}
		});
		return(selectSyscomboBox);
	}

	/**
	 * check that all the request details has been inserted
	 * @param sys - the system
	 * @param cur - current situation
	 * @param cha - Requested change
	 * @param rea - change reason
	 * @return Boolean
	 */
	private Boolean requestValidator(String sys,String cur,String cha,String rea){
		return(systemValidator(sys)||currentValidator(cur)||changeValidator(cha)||reasonValidator(rea));
	}
	
	/**
	 * 
	 * @param sys - the system
	 * @return Boolean
	 */
	private Boolean systemValidator(String sys){
		return(sys.equals("Select A System"));
	}
	
	/**
	 * 
	 * @param cur - current situation
	 * @return Boolean
	 */
	private Boolean currentValidator(String cur){
		return(cur.equals("Describe The Current Situation"));
	}
	
	/**
	 * 
	 * @param cha - Requested change
	 * @return Boolean
	 */
	private Boolean changeValidator(String cha){
		return(cha.equals("Describe The Wanted Change"));
	}
	
	/**
	 * 
	 * @param rea - change reason
	 * @return Boolean
	 */
	private Boolean reasonValidator(String rea){
		return(rea.equals("Descibe The Reason For Change"));
	}
	
	
	public JButton getBtnSend() {
		return btnSend;
	}

	public void setBtnSend(JButton btnSend) {
		btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!requestValidator((String)selectSyscomboBox.getSelectedItem(), curSitTxtArea.getText(), reqChangeTxtArea.getText(), reasonTxtArea.getText()))
					if (commentTxtArea.getText().equals("Mention Extra Information, If Needed"))
						p = new CProcessRequest(initiatorID,(String)selectSyscomboBox.getSelectedItem(), curSitTxtArea.getText(), reqChangeTxtArea.getText(), reasonTxtArea.getText());
					else p = new CProcessRequest(initiatorID,(String)selectSyscomboBox.getSelectedItem(), curSitTxtArea.getText(), reqChangeTxtArea.getText(), reasonTxtArea.getText(),commentTxtArea.getText());
				if(fileFlag==1) p.setFile(reqFile,fileEnd);
				NewRequestPack reqPack = new NewRequestPack(p);
				mainGui.client.handleMessageFromClientUI(reqPack);  //Send Pack To Server
				JOptionPane.showMessageDialog(serverGUI.mainframe,"New Request has been sent, You can track the request on button Track Request");
				newRequestMenuPanel.setVisible(false);
				newRequestPanel.setVisible(false);
			 }
			}
		);
		btnSend.setEnabled(false);
		btnSend.setEnabled(true);
		btnSend.setBounds(244, 435, 77, 23);
		newRequestPanel.add(btnSend);
	}
	
	
	public JButton getBtnAttachFile() {
		return btnAttachFile;
	}

	public void setBtnAttachFile(JButton btnAttachFile) {
		btnAttachFile = new JButton("Attach File");
		btnAttachFile.addActionListener(new ActionListener() {
			

			public void actionPerformed(ActionEvent e) {
	            JFileChooser chooser = new JFileChooser();  
	             chooser.showOpenDialog(null);                
	             selectedFile = chooser.getSelectedFile();               
	             String filename = selectedFile.getName();
	             System.out.println(filename);
	             System.out.println(selectedFile.getPath());
	             fileEnd = filename.substring(filename.lastIndexOf('.')+1).trim();
	             reqFile = fileUpload(filename,selectedFile.getPath());
			}
		});
		btnAttachFile.setBounds(10, 11, 103, 23);
		newRequestMenuPanel.add(btnAttachFile);
	}

	

	public MainGUI getMainGui() {
		return mainGui;
	}

	public void setMainGui(MainGUI mainGui) {
		this.mainGui = mainGui;
	}

	
	public MyFile fileUpload(String filename, String pathAndname){
		MyFile reqFile= new MyFile(filename);
		  String LocalfilePath=new String(pathAndname);
			this.fileFlag=1;
		  try{

			      File newFile = new File (LocalfilePath);
			      		      
			      byte [] mybytearray  = new byte [(int)newFile.length()];
			      FileInputStream fis = new FileInputStream(newFile);
			      BufferedInputStream bis = new BufferedInputStream(fis);			  
			    
			      reqFile.initArray(mybytearray.length);
			      reqFile.setSize(mybytearray.length);
			      
			      bis.read(reqFile.getMybytearray(),0,mybytearray.length);		      
			    }
			catch (Exception e) {
				System.out.println("Error send (Files)msg) to Server");
			}
		return reqFile;
	}
	/**
	 * 
	 */
	
}
